package com.answeringplayer.mapper;

import com.answeringplayer.dto.WrongQuestionDTO;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.time.LocalDateTime;
import java.util.List;

/**
 * 错题Mapper接口
 */
@Mapper
public interface WrongQuestionMapper {

    /**
     * 插入错题记录
     */
    int insert(WrongQuestionDTO wrongQuestion);
    /**
     * 根据用户ID和题目ID查询错题记录
     */
    WrongQuestionDTO selectByUserAndQuestion(
            @Param("userId") Long userId,
            @Param("questionId") Long questionId
    );
    /**
     * 根据ID查询错题记录
     */

    WrongQuestionDTO selectById(@Param("wrongId") Long wrongId);

    /**
     * 根据用户ID查询错题记录
     */
//    @Select("SELECT " +
//            "wrong_id as wrongId, " +
//            "user_id as userId, " +
//            "question_id as questionId, " +
//            "wrong_count as wrongCount, " +
//            "last_wrong_time as lastWrongTime, " +
//            "is_mastered as isMastered, " +
//            "created_at as createdAt, " +
//            "updated_at as updatedAt " +
//            "FROM wrong_question " +
//            "WHERE user_id = #{userId}")
    List<WrongQuestionDTO> selectByUserId(@Param("userId") Long userId);

    /**
     * 根据用户ID和题目ID查询错题记录
     */
    WrongQuestionDTO selectByUserIdAndQuestionId(@Param("userId") Long userId,
                                                 @Param("questionId") Long questionId);

    /**
     * 更新错题记录
     */
    int update(WrongQuestionDTO wrongQuestion);

    /**
     * 根据ID删除错题记录
     */
    int deleteById(@Param("wrongId") Long wrongId);

    /**
     * 根据用户ID分页查询错题记录
     */
    List<WrongQuestionDTO> selectByUserIdWithPagination(@Param("userId") Long userId,
                                                        @Param("offset") int offset,
                                                        @Param("limit") int limit);

    /**
     * 统计用户的错题数量
     */
    int countByUserId(@Param("userId") Long userId);

    /**
     * 根据时间范围查询错题记录
     */
    List<WrongQuestionDTO> selectByTimeRange(@Param("userId") Long userId,
                                             @Param("startTime") LocalDateTime startTime,
                                             @Param("endTime") LocalDateTime endTime);

    /**
     * 查询用户的高频错题（错误次数>=3）
     */
    List<WrongQuestionDTO> selectHighFrequencyWrongs(@Param("userId") Long userId);

    /**
     * 查询用户的未掌握错题
     */
    List<WrongQuestionDTO> selectUnmasteredWrongs(@Param("userId") Long userId);

    /**
     * 查询用户的近期错题（7天内）
     */
    List<WrongQuestionDTO> selectRecentWrongs(@Param("userId") Long userId);

    /**
     * 批量插入错题记录
     */
    int batchInsert(@Param("wrongQuestions") List<WrongQuestionDTO> wrongQuestions);

    /**
     * 增加错误次数
     */
    int incrementWrongCount(@Param("wrongId") Long wrongId);

    /**
     * 标记为已掌握
     */
    int markAsMastered(@Param("wrongId") Long wrongId);

    /**
     * 根据用户ID和题目ID标记为已掌握
     */
    int markAsMasteredByQuestionId(@Param("userId") Long userId,
                                   @Param("questionId") Long questionId);

    /**
     * 检查题目是否为用户的错题
     */
    boolean existsByUserIdAndQuestionId(@Param("userId") Long userId,
                                        @Param("questionId") Long questionId);
}
